package util.PluginTools;

import util.Info;

import javax.swing.*;
import java.awt.*;

/**
 * Created by hanyz on 2017/11/9.
 */
public class GetNameThead extends Thread {
	@Override
	public void run() {
		Component lastComponent = null;
		while (GetNameSwitch.GET_NAME) {
			try {
				Thread.sleep(2000);
				PointerInfo pointerInfo = MouseInfo.getPointerInfo();
				Point location = pointerInfo.getLocation();
				Container container = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
				SwingUtilities.convertPointFromScreen(location, container);
				Component component = container.findComponentAt(location);
				Container parent;
				if (component == null) {
					continue;
				}
				//用于嵌套控件
				if (component.getName() == null) {
					parent = component.getParent();
					if (component.getClass().getName().contains("$")) {
						component = parent;
					}
				}
				//javax.swing.plaf.basic.BasicComboBoxEditor$BorderlessTextField[,1,1,184x19,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@23bc54a,flags=25166112,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=109,g=109,b=109],editable=false,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],selectedTextColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],selectionColor=javax.swing.plaf.ColorUIResource[r=51,g=153,b=255],columns=9,columnWidth=6,command=,horizontalAlignment=LEADING]
				//避免停留重复输出和没有名字的输出
				if (component.getName() != null && (lastComponent == null || !lastComponent.equals(component))) {
					Info.out(component.getClass().getName() + ":" + component.getName());
					lastComponent = component;
				}
			} catch (Exception e) {
//                e.printStackTrace();
			}
		}
	}


}
